<template >
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="key" clearable @clear="onClearSearch">
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="searchUser">搜索标签</el-button>
        </el-col>
      </el-row>
      <el-table :data="article" v-loading="loading">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <div class="avatar-title-wrap">
              <img class="avatar" round fit="cover" :src="scope.row.cover_url" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="id" width="140">
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="120">
        </el-table-column>
        <el-table-column label="标题" prop="title">
        </el-table-column>
        <el-table-column label="内容">
          <template slot-scope="scope">
            <div class="content" v-html="scope.row.content">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <div slot-scope="scope">
            <el-button type="primary" icon="el-icon-view" size="mini" circle
              @click="$router.push('/article/' + scope.row.id)"></el-button>
            <el-button type="primary" icon="el-icon-edit" size="mini" circle
              @click="$router.push('/addarticle/' + scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="removeUserById(scope.row.id)">
            </el-button>
          </div>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="page.pageNumber" :page-size="5" layout="total, prev, pager, next, jumper" :total="page.total">
      </el-pagination>
    </el-card>
    <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogVisible = false">
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'User',
  data () {
    return {
      users: [],
      total: 0,
      currentPage: 1,
      pageSize: 6,
      key: '',
      editDialogVisible: false,
      activeName: 'first',
      article: [],
      searchForm: {
        label: ''
      },
      page: {
        pageSize: 5,
        pageNumber: 1,
        total: 0
      },
      loading: false
    }
  },
  created () {
    this.getArticleList()
  },
  methods: {
    async getArticleList () {
      this.loading = true
      const params = {
        label: this.searchForm.label,
        page: this.page
      }

      const res = await this.$http.post('/article/findArticle', params)
      this.article = res.data.result.rows.map(v => {
        v.content = v.content.replace(/<img/g, "<img style='height:30px'")
        return v
      })
      this.page.total = res.data.result.count

      this.loading = false
    },
    async searchUser () {
      const res = await this.$http.get('/article/searchlabel/' + this.key)
      console.log(res)
      this.article = res.data.result.rows
    },
    handleSizeChange (val) {
      this.page.pageSize = val
      this.getArticleList()
    },
    handleCurrentChange (val) {
      this.page.pageNumber = val
      this.getArticleList()
    },
    onClearSearch () {
      this.key = ''
      this.currentPage = 1
      this.getArticleList()
    },
    async showEditDialog (id) {
      // const { data: res } = await this.$http.get('users/' + id)
      // if (res.meta.status !== 200) {
      //   return this.$message.error('查询用户信息失败！')
      // }
      // this.editUserForm = res.data
      this.editDialogVisible = true
    },
    async removeUserById (id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文章, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 点击确定 返回值为：confirm
      // 点击取消 返回值为： cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const res = await this.$http.delete('/article/deleteArticle?articleId=' + id)
      if (res.data.code === 0) {
        this.$message.success('删除文章成功！')
        // window.history.reload()
        this.getArticleList()
      }
    }
    // handleClick (tab, event) {
    //   console.log(this.activeName)
    //   this.currentPage = 1
    //   this.getArticleList()
    // }

  }
}
</script>
<style lang="less" scoped>
.content {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-bottom: 40px;
}

.avatar-title-wrap {
  display: flex;
  align-items: center;

  .avatar {
    width: 40px;
    height: 40px;
    padding: 2px;
    background: #fff;
  }
}

    .el-breadcrumb {
      margin: 10px 0;
    }
</style>
